from info.constants import USER_COLLECTION_MAX_NEWS
from info.models import News, User
from info.modules.news import news_blu
from flask import render_template, current_app, session


@news_blu.route("/<int:news_id>")
def news_detail(news_id):


    #查询用户登录信息

    # 取出用户id
    user_id = session.get("user_id", None)
    # 查找模型
    user = None
    if user_id:
        try:
            user = User.query.get(user_id)
        except Exception as e:

            current_app.logger.error(e)

    # 右侧的新闻排行逻辑
    news_list = []

    try:
        news_list = News.query.order_by(News.clicks.desc()).limit(USER_COLLECTION_MAX_NEWS)

    except Exception as e:
        current_app.logger.error(e)

    news_dict_li = []
    # 遍历对象列表，将对象字典添加到字典列表中
    for news in news_list:
        news_dict_li.append(news.to_basic_dict())

    data = {

        "user": user.to_dict() if user else None,

        "news_dict_li": news_dict_li,


    }

    return render_template("news/detail.html",data = data)

